package com.globaldom.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.globaldom.model.CivResource;
import com.globaldom.model.Gift;
import com.globaldom.model.MilResource;
import com.globaldom.utils.Constants;
import com.globaldom.utils.PMF;
import com.globaldom.utils.SessionManager;

@Controller
@RequestMapping(value = "/gifts")
public class GiftsAddController {
	public GiftsAddController() {
	}
	// private static final Logger log =
	// Logger.getLogger(GiftsAddController.class.getName());

	/*
	 */
	@RequestMapping(value = "/send", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, String> addGifts(@RequestParam String giftid, @RequestParam(value = "persons") String persons,
			HttpSession session, HttpServletRequest request) {
		Map<String, String> map = new HashMap<String, String>();
		String personID = SessionManager.getID(request, session);
		if (null == personID) {
			map.put(Constants.STATUS, "-ER");
			map.put(Constants.CODE, "401");
			return map;
		}

		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Gift maingift = pm.getObjectById(Gift.class, giftid);

			if (!maingift.getGiftSender().equals(personID)) {
				throw new Exception("Not your gift!");
			}
			pm.deletePersistent(maingift);

			String[] personIds = persons.split("/");
			List<Gift> gifts = new ArrayList<Gift>();
			for (int i = 0; i < personIds.length; i++) {
				if (personIds[i].trim().length() < 1)
					continue;
				Gift gift = new Gift();
				gift.setGiftAmount(maingift.getGiftAmount());
				gift.setGiftItem(maingift.getGiftItem());
				gift.setGiftType(1);
				gift.setGiftSender(personID);
				gift.setGiftReceiver(personIds[i]);
				gifts.add(gift);
			}

			pm.makePersistentAll(gifts);
			map.put(Constants.STATUS, "+OK");

		} catch (Exception ex) {
			// log.severe("Error during transaction" + ex);
			map.put(Constants.STATUS, "+-ER");
			map.put(Constants.CODE, ex.getLocalizedMessage());
		} finally {
			pm.close();

		}

		return map;
	}

	@RequestMapping(value = "/accept", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, String> acceptGifts(@RequestParam(value = "gifts") String gifts,HttpServletRequest request, HttpSession session) {

		Map<String, String> map = new HashMap<String, String>();
		String personID = SessionManager.getID(request, session);
		if(null==personID) 
			{
			
			map.put(Constants.STATUS, "-ER");
			map.put(Constants.CODE, "401");
			return map;
			}

	
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			String[] giftIds = gifts.split("/");

			for (int i = 0; i < giftIds.length; i++) {
				if (giftIds[i].trim().length() < 1)
					continue;
				PersistenceManager pmf = PMF.get().getPersistenceManager();
				Gift gift = pmf.getObjectById(Gift.class, new Long(giftIds[i]));
				if (!personID.equals(gift.getGiftReceiver())) {
					continue;
				}
				int giftItem = gift.getGiftItem();

				if (giftItem < 7) {
					CivResource cv = pm.getObjectById(CivResource.class, personID + "/" + giftItem);
					long avail = cv.getResAvailable() + gift.getGiftAmount();
					cv.setResAvailable(avail);

				} else if (giftItem < 14) {
					giftItem = giftItem - 7;
					// set military resource here
					MilResource mil = pm.getObjectById(MilResource.class, personID + "/" + giftItem);
					long avail = mil.getMilAvailable() + gift.getGiftAmount();
					mil.setMilAvailable(avail);

				}

				pmf.deletePersistent(gift);
				pmf.close();

			}

			map.put(Constants.STATUS, "+OK");

		} catch (Exception ex) {
			// log.severe("Error during transaction" + ex);
			map.put(Constants.STATUS, "+-ER");
			map.put(Constants.CODE, ex.getLocalizedMessage());
		} finally {
			pm.close();
			// emf.close();
		}

		return map;
	}

}
